package com.icms.web.system.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.icms.system.service.IUserService;
import com.icms.system.vo.UserVo;


@Controller
@RequestMapping("/admin/system/user")
public class UserController {

	@Autowired
	private IUserService userService ;
	
	@RequestMapping(value={"", "index"}, method=RequestMethod.GET)
	public String user_main() {
		return "user_main" ;
	}
	
	@RequestMapping(value="/add_page", method=RequestMethod.GET)
	public String add_page() {
		return "user_form" ;
	}
	
	@RequestMapping(value="/add", method=RequestMethod.POST)
	public String add(UserVo user) {
		this.userService.add(user) ;
		return "user_main" ;
	}
	
	@RequestMapping(value="/{id}/delete", method=RequestMethod.GET)
	public String delete(@PathVariable String id) {
		this.userService.delete(id) ;
		return "index" ;
	}
	
	@RequestMapping(value="/{id}/get/{type}", method=RequestMethod.GET)
	public String get(@PathVariable String id, @PathVariable String type, Model model) {
		model.addAttribute("user", this.userService.get(id)) ;
		if("for_update".equals(type)) {
			return "user_form" ;
		} else {
			return "show_user" ;
		}
	}
	
	@RequestMapping(value="/{id}/get_json", method=RequestMethod.GET)
	public @ResponseBody UserVo get_json(@PathVariable String id) {
		return this.userService.get(id) ;
	}

	@RequestMapping(value="/update", method=RequestMethod.POST)
	public String update(UserVo user) {
		this.userService.update(user) ;
		return "user_main" ;
	}
	
	@RequestMapping(value="/users/json", method=RequestMethod.GET)
	public @ResponseBody List<UserVo> users(UserVo user) {
		return this.userService.datagrid(user) ;
	}
	
	@RequestMapping(value="/users/page", method=RequestMethod.GET)
	public String users(UserVo user, Model model) {
		model.addAttribute("users", this.userService.datagrid(user)) ;
		return "show_users" ;
	}
	
}
